interface Graphic {
    draw(at: Point): void
    handleMouse(event: Event): void
    getExtent(): Point
    load(from: IStream): void
    save(to: OStream): void

}

class OStream { }
class Point {
    declare x: number
    declare y: number
    constructor(x: number, y: number) {
        this.x = x
        this.y = y
    }
}
export class Image implements Graphic {

    constructor(file: string) {

    }
    draw(at: Point): void {
        console.log(`在${at.x},${at.y}位置画了一个图像`);
    }
    handleMouse(event: Event): void {
        throw new Error("Method not implemented.")
    }
    getExtent(): Point {
        throw new Error("Method not implemented.")
    }
    load(from: IStream): void {
        throw new Error("Method not implemented.")
    }
    save(to: OStream): void {
        throw new Error("Method not implemented.")
    }

}

class ImageProxy implements Graphic {

    declare private _image: Image | null
    declare private _extent: Point
    declare private _fileName: string
    private _origin = new Point(0, 0)
    constructor(fileName: string) {
        this._fileName = fileName
        this._extent = this._origin
        this._image = null
    }
    draw(at: Point): void {
        this.getImage().draw(at)
    }
    handleMouse(event: Event): void {
        this.getImage().handleMouse(event)
    }
    getExtent(): Point {
        if (this._extent === this._origin) {
            this._extent = this.getImage().getExtent()
        }
        return this._extent
    }
    load(from: IStream): void {
        throw new Error("Method not implemented.")
    }
    save(to: OStream): void {
        throw new Error("Method not implemented.")
    }
    getImage(): Image {
        if (this._image === null) {
            this._image = new Image(this._fileName)
        }
        return this._image
    }
}

class TextDocument {
    insert(graphic: Graphic) {
        graphic.draw(new Point(100, 100))
    }
}

const text = new TextDocument()
const img = new ImageProxy('c://a.img')
text.insert(img)